package com.icode.gulimall.order.web;

import com.icode.common.exception.NoStockException;
import com.icode.common.utils.R;
import com.icode.gulimall.order.entity.OrderReturnReasonEntity;
import com.icode.gulimall.order.feign.WareFeignService;
import com.icode.gulimall.order.service.OrderService;
import com.icode.gulimall.order.vo.FareVo;
import com.icode.gulimall.order.vo.OrderConfirmVo;
import com.icode.gulimall.order.vo.OrderSubmitVo;
import com.icode.gulimall.order.vo.SubmitOrderResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.math.BigDecimal;
import java.util.Date;
import java.util.UUID;
import java.util.concurrent.ExecutionException;

/**
 * @Author: my_code
 * @Email: my_code@foxmail.com
 * @Create: 2021-01-18 13:39
 */
@Slf4j
@Controller
public class OrderWebController {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    OrderService orderService;

    @Autowired
    WareFeignService wareFeignService;

    //todo 异常处理
    @GetMapping("/toTrade")
    public String toTrade(Model model) throws ExecutionException, InterruptedException {
        OrderConfirmVo orderConfirmVo = orderService.confirmOrder();
        model.addAttribute("confirmOrderData", orderConfirmVo);
        return "confirm";
    }

    /**
     * 商品下单
     * @param submitVo
     * @param model
     * @param attributes
     * @return
     */
    @PostMapping("/submitorder")
    public String orderSubmit(OrderSubmitVo submitVo, Model model, RedirectAttributes attributes) {
        try {
            SubmitOrderResponseVo submitOrderResponseVo = orderService.orderSubmit(submitVo);
            //下单成功来到支付选择页
            //下单失败回到订单确认页重新确定订单信息
            if (submitOrderResponseVo.getCode() == 0) {
                //成功
                model.addAttribute("submitOrderResp", submitOrderResponseVo);
                return "pay";
            } else {
                String msg = "下单失败";
                switch (submitOrderResponseVo.getCode()) {
                    case 1:
                        msg += "令牌订单信息过期，请刷新再次提交";
                        break;
                    case 2:
                        msg += "订单商品价格发生变化，请确认后再次提交";
                        break;
                }
                attributes.addFlashAttribute("msg", msg);
                return "redirect:http://order.gulimall.com/toTrade";
            }
        } catch (Exception e) {
            if (e instanceof NoStockException) {
                String message = ((NoStockException) e).getMessage();
                attributes.addFlashAttribute("msg", message);
            }
            return "redirect:http://order.gulimall.com/toTrade";
        }

    }

    @ResponseBody
    @GetMapping("/fare")
    public R fare(@RequestParam("addrId") Long addrId) {
        FareVo fareVo = orderService.getFare(addrId);
        return R.ok().setData(fareVo);
    }

    @ResponseBody
    @GetMapping("sendMsg")
    public String sendMessageTest() {
        OrderReturnReasonEntity reasonEntity = new OrderReturnReasonEntity();
        reasonEntity.setId(1L);
        reasonEntity.setCreateTime(new Date());
        reasonEntity.setName("reason");
        reasonEntity.setStatus(1);
        reasonEntity.setSort(2);
        String msg = "Hello World";
        //1、发送消息,如果发送的消息是个对象，会使用序列化机制，将对象写出去，对象必须实现Serializable接口

        //2、发送的对象类型的消息，可以是一个json
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend("hello-java-exchange", "hello1.java",
                    reasonEntity, new CorrelationData(UUID.randomUUID().toString()));
        }
        log.info("消息发送完成:{}", reasonEntity);
        return "ok";
    }
}
